{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "29caf8b0-df75-47e1-9a33-7a19d0472285",
      "metadata": {
        "id": "7d9bbf86da5e"
      },
      "outputs": [],
      "source": [
        "# Copyright 2023 Google LLC\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#     https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9a7417cf-48d8-4cf8-b6ef-793ecb90b424",
      "metadata": {
        "id": "93c8fc08a823"
      },
      "source": [
        "# Brochure Creation Tool using Imagen\n",
        "\n",
        "<table align=\"left\">\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\" alt=\"Google Colaboratory logo\"><br> Run in Colab\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fvision%2Fuse-cases%2Fbrochure-creation-using-imagen%2Fbrochure_creation_using_imagen.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://raw.githubusercontent.com/primer/octicons/refs/heads/main/icons/mark-github-24.svg\" alt=\"GitHub logo\"><br> View on GitHub\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\">\n",
        "      <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
        "    </a>\n",
        "  </td>\n",
        "</table>\n",
        "\n",
        "<div style=\"clear: both;\"></div>\n",
        "\n",
        "<b>Share to:</b>\n",
        "\n",
        "<a href=\"https://www.linkedin.com/sharing/share-offsite/?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/8/81/LinkedIn_icon.svg\" alt=\"LinkedIn logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://bsky.app/intent/compose?text=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7a/Bluesky_Logo.svg\" alt=\"Bluesky logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://twitter.com/intent/tweet?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/5a/X_icon_2.svg\" alt=\"X logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://reddit.com/submit?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://redditinc.com/hubfs/Reddit%20Inc/Brand/Reddit_Logo.png\" alt=\"Reddit logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/vision/use-cases/brochure-creation-using-imagen/brochure_creation_using_imagen.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\" alt=\"Facebook logo\">\n",
        "</a>            "
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0a9cdaf1-7d5d-45d0-9d69-b04e8a2f23cb",
      "metadata": {
        "id": "d4a65f25deb9"
      },
      "source": [
        "| | |\n",
        "|-|-|\n",
        "|Author(s) | [Sanchit Latawa](https://github.com/slatawa) , [Divya Veerapandian](https://github.com/divyapandian5)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b5afd460-a747-4cdf-88b6-527e3c9a8bf0",
      "metadata": {
        "id": "62d711d51653"
      },
      "source": [
        "# Brochure Creation Tool using Imagen\n",
        "\n",
        "A Generative AI driven tool utilizing Google Vertex AI and Imagen to create product Brochure material for promotional materials."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d58b81af-50b3-4bee-9c7f-9fc1c4b8b5e4",
      "metadata": {
        "id": "1837b473d07c"
      },
      "source": [
        "# Objectives\n",
        "\n",
        "- Create background images for product brochures\n",
        "- Place product image on top of the background along with text like company name\n",
        "- Add company logo image, if provided\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ce1e46b3-d016-4cf2-b222-7dbc7b4d7845",
      "metadata": {
        "id": "47cb031f6983"
      },
      "source": [
        "# Solution Architecture"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9e8d3990-0312-4052-9ecc-11022fec34df",
      "metadata": {
        "id": "eac0e84448d9"
      },
      "source": [
        "![SOlution](./architecture.png) "
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9d01b9e5-f874-4484-8486-d1e6f461ead6",
      "metadata": {
        "id": "1b936d5ba9d2"
      },
      "source": [
        "# Getting Started"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9c62f5b8-b77b-4e93-822d-4fd35ddcd777",
      "metadata": {
        "id": "6e5499de6aa4"
      },
      "source": [
        "# Install Vertex AI SDK & Other dependencies"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "3f65be7f-80cf-488f-ad61-4dd03410edc7",
      "metadata": {
        "id": "34e55dab6aff"
      },
      "outputs": [],
      "source": [
        "%pip install --upgrade --user -q google-cloud-aiplatform rembg Pillow opencv-python numpy requests gradio"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6b467ffe-cbf2-4380-ac41-55e44e403ccd",
      "metadata": {
        "id": "b68cac81ee83"
      },
      "source": [
        "Colab only: Run the following cell to restart the kernel. For Vertex AI Workbench you can restart the terminal using the button on top."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "22e68614-2ea8-45e9-9e3d-6270e5519d57",
      "metadata": {
        "id": "e83dbe61d880"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "\n",
        "if \"google.colab\" in sys.modules:\n",
        "    # Automatically restart kernel after installs so that your environment can access the new packages\n",
        "    import IPython\n",
        "\n",
        "    app = IPython.Application.instance()\n",
        "    app.kernel.do_shutdown(True)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "684475a1-6f55-45c6-ae97-3a2b5724d7dc",
      "metadata": {
        "id": "a2e3cfb57686"
      },
      "source": [
        "# Colab Only\n",
        "You will need to run the following cell to authenticates your Colab environment with your Google Cloud account."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "c9ee81a7-87cf-4bdc-97f0-b4e267aad85b",
      "metadata": {
        "id": "d5db67245e18"
      },
      "outputs": [],
      "source": [
        "if \"google.colab\" in sys.modules:\n",
        "    from google.colab import auth\n",
        "\n",
        "    auth.authenticate_user()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8e2cf91e-ec3d-4ed7-95cb-21e286e527af",
      "metadata": {
        "id": "fd002745cf8a"
      },
      "source": [
        "### Import Required Libraries"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 69,
      "id": "ea4c7f0f-14c0-4b8b-b466-c9d8133cf640",
      "metadata": {
        "id": "79735e0572f1"
      },
      "outputs": [],
      "source": [
        "from enum import Enum\n",
        "import os\n",
        "import random\n",
        "import time\n",
        "\n",
        "from IPython import display\n",
        "from PIL import Image\n",
        "from PIL import Image as PIL_Image\n",
        "from PIL import ImageColor\n",
        "import gradio as gr\n",
        "import matplotlib.pyplot as plt\n",
        "from rembg import remove\n",
        "import vertexai\n",
        "import vertexai.preview.generative_models as generative_models\n",
        "from vertexai.preview.vision_models import ImageGenerationModel"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1be6fb12-7a4d-4b92-bd77-78b55ffcc0c5",
      "metadata": {
        "id": "fb97edd73fac"
      },
      "source": [
        "### ALl set lets run the pipeline \n",
        "\n",
        "Set these variables below:\n",
        "\n",
        "1. Enter `YOUR_PROJECT_ID` in project_id\n",
        "2. Enter `REGION` in REGION"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e61e1079-2ddf-4861-be64-eef795516c32",
      "metadata": {
        "id": "ea9e343efbc0"
      },
      "source": [
        "# Env Variables"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "72f91dcb-1dce-4613-9f64-681c6a38a14a",
      "metadata": {
        "id": "e475b5cda55c"
      },
      "outputs": [],
      "source": [
        "PROJECT_ID = \"[your-project-id]\"\n",
        "# \"sl-test-project-353312\"\n",
        "REGION = \"[your-region]\"\n",
        "IMAGE_MODEL_NAME = \"imagen-3.0-generate-002\"\n",
        "# \"us-central1\"\n",
        "vertexai.init(project=PROJECT_ID, location=REGION)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "892502a4-392f-4dbc-89f4-35b7b85d910b",
      "metadata": {
        "id": "c7ee99a005ff"
      },
      "source": [
        "### Get token"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "84ad32d9-b8f8-4bcb-bb04-72279d4f977b",
      "metadata": {
        "id": "02618a5122f8"
      },
      "source": [
        "### Sample Background prompts to choose from \n",
        "\n",
        "These prompts would be used to generate Image using Imagen and used as background of the product."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "70256428-a5a4-4ff9-b112-ee3c32bf53be",
      "metadata": {
        "id": "229bbb92a253"
      },
      "outputs": [],
      "source": [
        "EXAMPLE_PROMPTS = [\n",
        "    \"\"\"Display a vibrant, high-resolution background illuminated by a combination of soft, diffused light and a touch of dramatic side lighting.The background consists of a clean, minimalist setting with subtle geometric patterns\"\"\",\n",
        "    \"\"\"Nature's Touch Neutral beige background with a subtle gradient from a light brown to an off-white A faint, organic pattern of leaves and branches creates a natural and serene ambiance The pattern is intentionally blurred to prevent it from overpowering the product\"\"\",\n",
        "    \"\"\"Modern Simplicity Clean, white background with a soft gradient from a light blue to a pale gray Minimalistic lines form an abstract, geometric pattern in the background The pattern is subtle and fades into the background, allowing the product to take center stage\"\"\",\n",
        "    \"\"\"Neutral Paper with Faint Linen Texture A neutral beige background resembling paper or canvas, providing a warm and inviting backdrop. A faint linen texture adds a touch of sophistication and depth, creating a subtle visual interest. The background conveys a sense of timeless elegance and quality, reflecting Company's commitment to craftsmanship and enduring products.\"\"\",\n",
        "    \"\"\"Soft Pastel Gradient with Organic Shapes A soft gradient background transitioning between steel and blue, creating a sense of tranquility and optimism. Organic shapes, such as flowing lines or abstract curves, add a touch of movement and playfulness. The background evokes a sense of harmony, innovation, and a brighter future, embodying Company's dedication to societal advancement.\"\"\",\n",
        "    \"\"\"Light of the Future\"*** **Minimalist Design:** Simple, flowing curves convey a sense of progress and fluidity.* **Subtle Gradient:** A subtle gradient from light blue to white creates a luminous backdrop, suggesting a bright future ahead.* **Neutral Colors:** Light blue and white provide a clean and airy neutral background.* **Subtle Pattern:** A faint geometric pattern of hexagons symbolizes technology and connectivity, reflecting Company's problem-solving nature.* **Company's Philosophy:** The flowing curves evoke a sense of optimism and a journey toward a brighter tomorrow.\"\"\",\n",
        "]\n",
        "FUTURISTIC_EXAMPLE_PROMPTS = [\n",
        "    \"\"\"The background features a pristine, ultra-high-definition image of a CVD Coated Carbide surface. The surface is meticulously rendered with a high degree of precision, showcasing its smooth, lustrous finish.\"\"\",\n",
        "    \"\"\"A grid of abstract electronic circuit patterns in neon hues, forming a dynamic and futuristic backdrop\"\"\",\n",
        "    \"\"\"A seamless blend of organic and technological imagery against a matte gray background..\"\"\",\n",
        "    \"\"\"Crisp blue gradient background reminiscent of a clear night sky.\"\"\",\n",
        "    \"\"\"Subtle geometric shapes in soft blue and white, representing the harmony and fusion of technology and nature\"\"\",\n",
        "    \"\"\"Delicately sketched soft light blue and white circuit patterns interwoven into the gradient, resembling ethereal constellations\"\"\",\n",
        "    \"\"\"A shimmering, metallic horizon line at the base of the image, representing the intersection between cutting-edge technology and the boundless possibilities of the future.\"\"\",\n",
        "    \"\"\"A vast, white expanse stretching out to the horizon, with faint blue lines representing connectivity and growth.\"\"\",\n",
        "    \"\"\"Concept : Binary Landscape Image: A minimalist landscape composed of binary code that gradually morphs into intricate patterns. Color Palette: Blue and white, with subtle hints of gray to convey the contrast between technology and nature. Technical Specifications: 4K resolution, dynamic motion to symbolize the constant evolution of technology.\"\"\",\n",
        "    \"\"\"Concept : Problem-Solving Matrix Image: An abstract grid of interconnected nodes and lines. The grid represents a complex problem-solving space. Color Palette: White with accents of blue Futuristic Element: The grid is constantly shifting and adapting, symbolizing Company's iterative approach to problem-solving. Company's Philosophy: The image highlights Company's dedication to finding innovative solutions through meticulous analysis and collaboration. Color Palette: Bright blue and white, emphasizing the idea of unity and collaboration. Technical Specifications: 4K resolution, close-up perspective for an intimate and impactful connection.\"\"\",\n",
        "]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fa7170c4-f3a1-47da-802f-d5c313e213de",
      "metadata": {
        "id": "b2b7f10d56ce"
      },
      "source": [
        "### Use this code to get possible prompt to generate an Image background. Tune the prompt based on your idea\n",
        "\n",
        "The below code uses Gemini to generate a prompt which can then be used with Imagen to generate a background image for the product brochure."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 63,
      "id": "94d4af83-9f70-4592-b45e-4553b016e750",
      "metadata": {
        "id": "2c4f0fd1839c"
      },
      "outputs": [],
      "source": [
        "def generate_imagen_prompt_using_llm(\n",
        "    temperature: str,  # \"Cool\", \"Warm\", or \"Neutral\"\n",
        "    colors: str,  # Comma-separated string of colors\n",
        "    is_blurred: bool,  # \"True\" or \"False\"\n",
        "    patterns: str,  # String representing a single pattern\n",
        "    style: str,  # String representing the style\n",
        ") -> str:\n",
        "    model = generative_models.GenerativeModel(\"gemini-2.0-flash\")\n",
        "\n",
        "    add_blur_text = \"Blur the generated Image\" if is_blurred else \"\"\n",
        "\n",
        "    llm_prompt = f\"\"\"Background: \n",
        "    As a distinguished marketing expert renowned for crafting captivating visuals, you excel in transforming intricate concepts into sleek, minimalist imagery tailored for brochures, PR materials, and sales collateral.Your expertise shines particularly bright in the electronics industry.\n",
        "    Client: Company, a global technology trailblazer, epitomizes a profound dedication to leveraging technology for societal advancement. Their ethos underscores a commitment to shaping a better tomorrow through innovation and problem-solving. Company unveils a cutting-edge product and seeks your adept touch for a forward-thinking PR campaign. \n",
        "    \n",
        "    Challenge: Your task is to conceive one background image concept that embody the following principles:\n",
        "    Colors to be used: shades of {colors}\n",
        "    Temperature: {temperature}\n",
        "    Pattern to be used: {patterns}\n",
        "    Style of the image: {style}\n",
        "    Minimalist Design: Craft backgrounds with clean lines and simple shapes, eschewing clutter or intricate patterns that may divert attention from the product.\n",
        "    Subtle Gradient: Infuse backgrounds with gentle gradients, seamlessly transitioning between hues to lend depth without overpowering the focal point. These gradients should evoke a sense of dimensionality, subtly enhancing the overall visual appeal.\n",
        "    Subtle Texture or Pattern: Introduce faint patterns or subtle textures such as linen or paper to add visual interest without detracting from the product\\'s prominence.\n",
        "    Additionally, your designs should reflect Company\\'s philosophy, embodying their commitment to a brighter future through technological innovation and problem-solving. \n",
        "    Ensure your backgrounds convey a sense of optimism, progress, and societal betterment.  Lastly, aim for high-quality, 4K resolution images to meet the client\\'s expectations \n",
        "    for technical specifications. {add_blur_text}\"\"\"\n",
        "\n",
        "    generation_config = generative_models.GenerationConfig(\n",
        "        max_output_tokens=2048, temperature=0.9, top_p=1\n",
        "    )\n",
        "\n",
        "    safety_settings = {\n",
        "        generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,\n",
        "        generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,\n",
        "        generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,\n",
        "        generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,\n",
        "    }\n",
        "\n",
        "    # Neutral Colors: Opt for soft gray, light blue, beige, or pastel tones to provide a neutral backdrop that accentuates the product without overshadowing it.\n",
        "    response = model.generate_content(\n",
        "        llm_prompt, generation_config=generation_config, safety_settings=safety_settings\n",
        "    )\n",
        "\n",
        "    return response.text"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "63faebca-961a-49d3-99e3-df38608f7c21",
      "metadata": {
        "id": "632598d9ceea"
      },
      "source": [
        "### Test Prompt creation using LLM for Imagen"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 64,
      "id": "79fb57a4-cea4-4f1b-9207-69a468de3503",
      "metadata": {
        "id": "20648b74899b"
      },
      "outputs": [],
      "source": [
        "temperature = \"Cool\"  ##  warm , cool ,neutral\n",
        "colors = \"white,blue\"\n",
        "is_blurred = False\n",
        "patterns = \"waves\"\n",
        "style = \"Minimalistic\"\n",
        "\n",
        "prompt_for_imagen = generate_imagen_prompt_using_llm(\n",
        "    temperature, colors, is_blurred, patterns, style\n",
        ")\n",
        "print(prompt_for_imagen)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4d21c73a-04e8-4b6c-828c-4f4b79e62ba8",
      "metadata": {
        "id": "6a39d5883bf8"
      },
      "source": [
        "## Methods required to Generate Image using Imagen API"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 65,
      "id": "faf53cff-5573-4cd1-b695-730392566422",
      "metadata": {
        "id": "294d4b2970d7"
      },
      "outputs": [],
      "source": [
        "def imagen_generate_sdk(\n",
        "    model_name: str,\n",
        "    prompt: str,\n",
        "    negativePrompt: str,\n",
        "    sampleCount: int,\n",
        "    seed: int | None = None,\n",
        "    disablePersonFace: bool | None = False,  # Whether to disable generating faces\n",
        "    sampleImageStyle: str | None = None,\n",
        ") -> list[Image.Image]:\n",
        "    model = ImageGenerationModel.from_pretrained(model_name)\n",
        "    response = model.generate_images(\n",
        "        prompt=prompt, number_of_images=sampleCount, negative_prompt=negativePrompt\n",
        "    )\n",
        "\n",
        "    pillow_images = []\n",
        "    for image in response.images:\n",
        "        # image_bytes = io.BytesIO(image._image_bytes)  # Convert to in-memory file\n",
        "        # pillow_images.append(Image.open(image_bytes))\n",
        "        pillow_images.append(image._pil_image)\n",
        "    return pillow_images\n",
        "\n",
        "\n",
        "# Images should be an array of images in PIL image format\n",
        "def display_images(pil_images):\n",
        "    scale = 0.25\n",
        "    sampleImageSize = 1536\n",
        "    width = int(float(sampleImageSize) * scale)\n",
        "    height = int(float(sampleImageSize) * scale)\n",
        "    for index, result in enumerate(pil_images):\n",
        "        width, height = pil_images[index].size\n",
        "        print(index)\n",
        "        display.display(\n",
        "            pil_images[index].resize(\n",
        "                (\n",
        "                    int(pil_images[index].size[0] * scale),\n",
        "                    int(pil_images[index].size[1] * scale),\n",
        "                ),\n",
        "                0,\n",
        "            )\n",
        "        )\n",
        "        print()\n",
        "\n",
        "\n",
        "def display_image(image):\n",
        "    plt.imshow(image, cmap=\"gray\")\n",
        "    plt.axis(\"off\")  # Turn off axes\n",
        "    plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a9169e11-2f56-4d6d-8a51-cf07bd39a86b",
      "metadata": {
        "id": "2ab585e8a42c"
      },
      "source": [
        "## Methods required to make Brochure"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 66,
      "id": "05430349-3845-4c11-a0fe-569ebf480bca",
      "metadata": {
        "id": "a5849fb72846"
      },
      "outputs": [],
      "source": [
        "from PIL import Image, ImageColor\n",
        "\n",
        "\n",
        "def make_brochure(\n",
        "    subject_image: Image.Image,\n",
        "    bg_image: Image.Image,\n",
        "    logo_image: Image.Image,\n",
        "    banner_color: tuple[int, int, int],  # Assuming RGB tuple\n",
        ") -> Image.Image:\n",
        "    # Call overlay to superimpose product image with background\n",
        "    brochure_image = overlay(subject_image, bg_image, \"middle right\", None)\n",
        "\n",
        "    # Define the size of the blue banners\n",
        "    banner_height = 100  # Height of the banners in pixels\n",
        "    # banner_color = (41,106,145)  # Teal Blue color for the banners\n",
        "    banner_image = Image.new(\"RGB\", (842, 100), banner_color)\n",
        "\n",
        "    # Place the banner images on the top and bottom of the background image\n",
        "    brochure_image.paste(banner_image, (0, 0))\n",
        "    brochure_image.paste(banner_image, (0, brochure_image.height - banner_image.height))\n",
        "\n",
        "    # Add Company Logo\n",
        "    final_image = add_logo(brochure_image, logo_image, \"top left\", 0, banner_height)\n",
        "\n",
        "    return final_image\n",
        "\n",
        "\n",
        "def add_logo(image, logo, corner, padding, banner_height):\n",
        "    \"\"\"Adds the Company logo to one of the corners of an image.\"\"\"\n",
        "    # logo = remove(logo, alpha_matting = True,alpha_matting_foreground_threshold=40) # remove background\n",
        "    logo = logo.resize(\n",
        "        (int(image.width / 6), banner_height)\n",
        "    )  ##(int(image.height * 0.1), int(image.height * 0.1)))\n",
        "    logo = logo.convert(\"RGBA\")\n",
        "    if corner == \"top left\":\n",
        "        image.paste(logo, (padding, padding), logo)\n",
        "    elif corner == \"top right\":\n",
        "        image.paste(logo, (image.width - logo.width - padding, padding), logo)\n",
        "    elif corner == \"bottom left\":\n",
        "        image.paste(logo, (padding, image.height - logo.height - padding), logo)\n",
        "    elif corner == \"bottom right\":\n",
        "        image.paste(\n",
        "            logo,\n",
        "            (image.width - logo.width - padding, image.height - logo.height - padding),\n",
        "            logo,\n",
        "        )\n",
        "    return image\n",
        "\n",
        "\n",
        "class Position(Enum):\n",
        "    TOP_RIGHT = \"top right\"\n",
        "    TOP_CENTER = \"top center\"\n",
        "    TOP_LEFT = \"top left\"\n",
        "    MIDDLE_RIGHT = \"middle right\"\n",
        "    CENTER = \"center\"\n",
        "    MIDDLE_LEFT = \"middle left\"\n",
        "    BOTTOM_RIGHT = \"bottom right\"\n",
        "    BOTTOM_CENTER = \"bottom center\"\n",
        "    BOTTOM_LEFT = \"bottom left\"\n",
        "\n",
        "\n",
        "def overlay(subject_image, bg_img, position, bgColor):\n",
        "    subject_image = subject_image.resize(\n",
        "        tuple(int(ti / 4) for ti in subject_image.size)\n",
        "    )\n",
        "    subject_image_bg_removed = remove(\n",
        "        subject_image, alpha_matting=True, bgcolor=bgColor\n",
        "    )  # remove background\n",
        "    ## in proportion to A4 size\n",
        "    bg_img = bg_img.resize((842, 1191))\n",
        "\n",
        "    if position == Position.TOP_RIGHT.value:\n",
        "        x = bg_img.width - subject_image_bg_removed.width\n",
        "        y = 0\n",
        "    elif position == Position.TOP_CENTER.value:\n",
        "        x = (bg_img.width - subject_image_bg_removed.width) // 2\n",
        "        y = 0\n",
        "    elif position == Position.TOP_LEFT.value:\n",
        "        x = 0\n",
        "        y = 0\n",
        "    elif position == Position.MIDDLE_RIGHT.value:\n",
        "        x = bg_img.width - subject_image_bg_removed.width - 30\n",
        "        y = (bg_img.height - subject_image_bg_removed.height) // 2\n",
        "    elif position == Position.CENTER.value:\n",
        "        x = (bg_img.width - subject_image_bg_removed.width) // 2\n",
        "        y = (bg_img.height - subject_image_bg_removed.height) // 2\n",
        "    elif position == Position.MIDDLE_LEFT.value:\n",
        "        x = 30\n",
        "        y = (bg_img.height - subject_image_bg_removed.height) // 2\n",
        "    elif position == Position.BOTTOM_RIGHT.value:\n",
        "        print(\"came in middle right\")\n",
        "        x = bg_img.width - subject_image_bg_removed.width\n",
        "        y = bg_img.height - subject_image_bg_removed.height\n",
        "    elif position == Position.BOTTOM_CENTER.value:\n",
        "        x = (bg_img.width - subject_image_bg_removed.width) // 2\n",
        "        y = bg_img.height - subject_image_bg_removed.height\n",
        "    elif position == Position.BOTTOM_LEFT.value:\n",
        "        x = 0\n",
        "        y = bg_img.height - subject_image_bg_removed.height\n",
        "\n",
        "    bg_img.paste(subject_image_bg_removed, (x, y), subject_image_bg_removed)\n",
        "    return bg_img"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a5d9142f-257a-4791-843b-bd1335c0426d",
      "metadata": {
        "id": "1e591b91d593"
      },
      "source": [
        "## Wrapper call to instantiate required variables and call brochure generation "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 47,
      "id": "259b4bfc-c7dd-40da-ac17-aa9fc189a0cf",
      "metadata": {
        "id": "8a1b4ae7bb71"
      },
      "outputs": [],
      "source": [
        "def call_brochure_maker(\n",
        "    prompt, negative_prompt, logo_image, subject_image, banner_color, sampleResultCount\n",
        "):\n",
        "    sampleCount = int(sampleResultCount)\n",
        "    negativePrompt = \"faces, people, animals, food, large objects\"\n",
        "\n",
        "    # Advanced option, try different the seed numbers any random integer number range: (0, 2147483647)\n",
        "    seed = None\n",
        "    sampleImageStyle = None\n",
        "\n",
        "    images = imagen_generate_sdk(\n",
        "        IMAGE_MODEL_NAME,\n",
        "        prompt,\n",
        "        negativePrompt,\n",
        "        sampleCount,\n",
        "        seed,\n",
        "        False,\n",
        "        sampleImageStyle,\n",
        "    )\n",
        "    # display_images(images)\n",
        "\n",
        "    # Get the current timestamp, # Generate a random number # Concatenate timestamp and random number to create a unique number\n",
        "    current_timestamp = int(time.time())\n",
        "    random_number = random.randint(1000, 9999)\n",
        "    unique_number = int(str(current_timestamp) + str(random_number))\n",
        "    directory_path = \"results/\" + str(unique_number)\n",
        "    if not os.path.exists(directory_path):\n",
        "        # Create the directory\n",
        "        os.makedirs(directory_path)\n",
        "\n",
        "    results = []\n",
        "    for bg_image in images:\n",
        "        final_image = make_brochure(subject_image, bg_image, logo_image, banner_color)\n",
        "        results.append(final_image)\n",
        "    return results"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "efaa9553-5da4-4f1f-80fd-457f43fbdaa1",
      "metadata": {
        "id": "59831c438730"
      },
      "source": [
        "### TEST - Call the Wrapper to call background generation, add Banner, logo and create Brochure "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 67,
      "id": "f192e98e-79ca-42c0-b783-eba28f94a4e6",
      "metadata": {
        "id": "6c038b0e2e15"
      },
      "outputs": [],
      "source": [
        "fg_path = \"./S-DRV-4D.jpg\"\n",
        "logo_path = \"./company_logo.jpeg\"\n",
        "banner_color = \"teal\"\n",
        "subject_image = Image.open(fg_path)  # load image\n",
        "logo_image = Image.open(logo_path)  # load image\n",
        "sample_resultcount = 3\n",
        "results = call_brochure_maker(\n",
        "    \"beautiful underwater image of sea in the background, subtle\",\n",
        "    \"faces\",\n",
        "    logo_image,\n",
        "    subject_image,\n",
        "    banner_color,\n",
        "    sample_resultcount,\n",
        ")\n",
        "\n",
        "for res in results:\n",
        "    display_image(res)\n",
        "    res.save(\"brochure.jpg\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6c76bc41-102c-47e2-b373-7510bef83733",
      "metadata": {
        "id": "8c06848dfbcb"
      },
      "source": [
        "### TEST - End to End Flow"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 68,
      "id": "df8dceae-b77a-4136-ba08-2e4f8051d203",
      "metadata": {
        "id": "0cb29a555f46"
      },
      "outputs": [],
      "source": [
        "fg_path = \"./S-DRV-4D.jpg\"\n",
        "logo_path = \"./company_logo.jpeg\"\n",
        "banner_color = \"teal\"\n",
        "subject_image = Image.open(fg_path)  # load image\n",
        "logo_image = Image.open(logo_path)  # load image\n",
        "sample_resultcount = 3\n",
        "\n",
        "\n",
        "temperature = \"Cool\"  ##  warm , cool ,neutral\n",
        "colors = \"white,blue\"\n",
        "is_blurred = \"No\"\n",
        "patterns = \"waves\"\n",
        "style = \"Minimalistic\"\n",
        "\n",
        "# generate Prompt\n",
        "prompt_for_imagen = generate_imagen_prompt_using_llm(\n",
        "    temperature, colors, is_blurred, patterns, style\n",
        ")\n",
        "\n",
        "# Generate Brochure\n",
        "results = call_brochure_maker(\n",
        "    prompt_for_imagen,\n",
        "    \"faces\",\n",
        "    logo_image,\n",
        "    subject_image,\n",
        "    banner_color,\n",
        "    sample_resultcount,\n",
        ")\n",
        "\n",
        "for res in results:\n",
        "    res.show()\n",
        "    res.save(\"brochure.jpg\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fa2e537b-2009-413f-a995-7bfb3a51af80",
      "metadata": {
        "id": "0d8d05a5f025"
      },
      "source": [
        "### TEST - Call just the brochure maker with the background image of your choice to test "
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2221709e-f366-4e59-bdfc-de76084437bb",
      "metadata": {
        "id": "66c2a0501894"
      },
      "source": [
        "# Sample UI App fro Demo"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9900667a-9ef5-4f53-8592-bc07973b1157",
      "metadata": {
        "id": "b3945e0501f5"
      },
      "outputs": [],
      "source": [
        "banner_color_options = []\n",
        "for color in ImageColor.colormap:\n",
        "    banner_color_options.append(color)\n",
        "\n",
        "\n",
        "def image_process_call_brochure_maker(\n",
        "    background_context,\n",
        "    negative_prompt,\n",
        "    banner_color,\n",
        "    logo_image,\n",
        "    product_image,\n",
        "    sample_resultcount,\n",
        "):\n",
        "    subject_image = PIL_Image.fromarray(product_image)\n",
        "    logo_image_pil = PIL_Image.fromarray(logo_image)\n",
        "\n",
        "    images_list_results = call_brochure_maker(\n",
        "        background_context,\n",
        "        negative_prompt,\n",
        "        logo_image_pil,\n",
        "        subject_image,\n",
        "        banner_color,\n",
        "        sample_resultcount,\n",
        "    )\n",
        "    return images_list_results\n",
        "\n",
        "\n",
        "with gr.Blocks() as demo:\n",
        "    with gr.Row():\n",
        "        gr.Markdown(\"# Brochure Maker\")\n",
        "    with gr.Row():\n",
        "        with gr.Column(min_width=100):\n",
        "            gr.Markdown(\"## Prompt Generator\")\n",
        "            temperature = gr.Dropdown(\n",
        "                choices=[\"Warm\", \"Cool\", \"Neutral\"],\n",
        "                label=\"Temperature\",\n",
        "                info=\"Color temperature or light appearance on a cool to warm scale\",\n",
        "            )\n",
        "            colors = gr.Textbox(\n",
        "                label=\"Colors you want to see eg; white, blue or green, yellow\"\n",
        "            )\n",
        "            patterns = gr.Textbox(\n",
        "                label=\"Any patterns you want to see eg: lines, geometric shapes, leaves, circles\"\n",
        "            )\n",
        "            style = gr.Dropdown(\n",
        "                choices=[\"Abstract\", \"Vintage\", \"Modern\", \"Minimalistic\"], label=\"Style\"\n",
        "            )\n",
        "            is_blurred = gr.Dropdown(choices=[\"Yes\", \"No\"], label=\"Blurred ?\")\n",
        "            submit_button = gr.Button(\"Show Prompts\")\n",
        "            llm_image_prompts = gr.Textbox(label=\"Output Prompt(s)\")\n",
        "        with gr.Column(min_width=100):\n",
        "            gr.Markdown(\"## Brochure Maker\")\n",
        "            with gr.Row():\n",
        "                with gr.Column(min_width=100):\n",
        "                    background_context = gr.Textbox(\n",
        "                        label=\"Enter your prompt for image background\"\n",
        "                    )\n",
        "                    negative_prompt = gr.Textbox(\n",
        "                        label=\"Anything you want to avoid seeing in the image? eg: forest, plants, car\"\n",
        "                    )\n",
        "                    banner_color = gr.Dropdown(\n",
        "                        choices=banner_color_options,\n",
        "                        label=\"Banner color\",\n",
        "                        info=\"Will appear on the top and bottom\",\n",
        "                    )\n",
        "                    product_image = gr.Image(label=\"Product Image\")\n",
        "                    logo_image = gr.Image(label=\"Logo Image\")\n",
        "                    sample_resultcount = gr.Textbox(\n",
        "                        label=\"How many images you want to see? 1-4\"\n",
        "                    )\n",
        "\n",
        "            with gr.Row():\n",
        "                with gr.Row():\n",
        "                    with gr.Column(min_width=50):\n",
        "                        generate_btn = gr.Button(\"Generate\")\n",
        "                    with gr.Column(min_width=50):\n",
        "                        clear = gr.ClearButton()\n",
        "    with gr.Row():\n",
        "        with gr.Column():\n",
        "            with gr.Column():\n",
        "                with gr.Row():\n",
        "                    bg_gallery = gr.Gallery()\n",
        "\n",
        "    # clear.add([bg_gallery, background_context, negative_prompt, banner_color,logo_image, product_image])\n",
        "    def on_click(\n",
        "        background_context,\n",
        "        negative_prompt,\n",
        "        banner_color,\n",
        "        logo_image,\n",
        "        product_image,\n",
        "        sample_resultcount,\n",
        "    ):\n",
        "        images_list = image_process_call_brochure_maker(\n",
        "            background_context=background_context,\n",
        "            negative_prompt=negative_prompt,\n",
        "            banner_color=banner_color,\n",
        "            logo_image=logo_image,\n",
        "            product_image=product_image,\n",
        "            sample_resultcount=sample_resultcount,\n",
        "        )\n",
        "        return gr.Gallery(images_list)\n",
        "\n",
        "    generate_btn.click(\n",
        "        fn=on_click,\n",
        "        inputs=[\n",
        "            background_context,\n",
        "            negative_prompt,\n",
        "            banner_color,\n",
        "            logo_image,\n",
        "            product_image,\n",
        "            sample_resultcount,\n",
        "        ],\n",
        "        outputs=[bg_gallery],\n",
        "    )\n",
        "\n",
        "    submit_button.click(\n",
        "        fn=generate_imagen_prompt_using_llm,\n",
        "        inputs=[temperature, colors, is_blurred, patterns, style],\n",
        "        outputs=[llm_image_prompts],\n",
        "    )\n",
        "\n",
        "demo.launch(debug=True, share=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fe6bdccc-4b76-4e0f-a5df-4bcf7140ca81",
      "metadata": {
        "id": "9b154c0bc096"
      },
      "source": [
        "# Conclusion\n",
        "\n",
        "In this notebook we have successfully used Google Cloud's Generative AI capabilities to generate background images and tie them together to generate a Product Brochure. Along with that we also created a sample UI to test the pipeline end to end."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "brochure_creation_using_imagen.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
